/**
 * <p><b>Title:</b> AuthorFilter.java </p>
 * @Package com.wondertek.oes.charge.service.manager.rule.filter
 */
package com.wondertek.poms.core.filter.impl;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.wondertek.poms.core.filter.BaseRuleFilter;
import com.wondertek.poms.core.filter.RuleFilterChain;
import org.apache.commons.lang3.StringUtils;

import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.DefaultPrdSal;
import com.wondertek.poms.dao.po.IRulePo;


/**
 * <p><b>AuthorFilter Description:</b> 关键字匹配规则</p>
 * @author douzi
 * <b>DATE</b> 2019年8月30日 下午2:28:13
 */
public class KeyWordFilter extends BaseRuleFilter {

	@Override
	public void doFilter(IRulePo rule, Content c, RuleFilterChain chain) {
		if (rule instanceof DefaultPrdSal) {
			DefaultPrdSal r = (DefaultPrdSal) rule;
			this.process(r.getId(),
					r.getKeyWord(),
					chain,
					"关键字-PrimaryKeyWord&KeyWords", c);
		}
		chain.doFilter(rule, c, chain);
	}
	
	/**
	 * <p><b>Title:</b> process</p>
	 * <p><b>Description:</b> DefaultPrdSal 默认计费过滤方法</p>
	 * @author douzi
	 * @param prdRuleId
	 * @param secondClass
	 * @param secondClassifyList
	 * @param chain
	 * @param comments
	 * @param c
	 */
	private void process(Long prdRuleId, String keyWord, RuleFilterChain chain, String comments, Content c) {
		if (chain.isFlag() && StringUtils.isNotBlank(keyWord)) {
			chain.setFlag(filterSClassify(prdRuleId, keyWord, c));
            log(prdRuleId, chain, comments,  keyWord, c.getPrimaryKeyWord() + "," + c.getKeyWords());
        }
	}

	private boolean filterSClassify(Long prdRuleId, String keyWord, Content cont) {
		boolean flag = false;
		
		List<String> contentKeyWords = new ArrayList<String>();
		if (cont.getKeyWords() != null && !"".equals(cont.getKeyWords())) {
			String[] normalKeyWordArgs = cont.getKeyWords().replaceAll("，", ",").split(",");
			if (normalKeyWordArgs != null && normalKeyWordArgs.length > 0) {
				for (int i = 0; i < normalKeyWordArgs.length; i++) {
					if (normalKeyWordArgs[i] != null && !"".equals(normalKeyWordArgs[i].trim())) {
						contentKeyWords.add(normalKeyWordArgs[i].trim());
					}
				}
			}
		}
		if (cont.getPrimaryKeyWord() != null && !"".equals(cont.getPrimaryKeyWord())) {
			String[] normalKeyWordArgs = cont.getPrimaryKeyWord().replaceAll("，", ",").split(",");
			if (normalKeyWordArgs != null && normalKeyWordArgs.length > 0) {
				for (int i = 0; i < normalKeyWordArgs.length; i++) {
					if (normalKeyWordArgs[i] != null && !"".equals(normalKeyWordArgs[i].trim())) {
						contentKeyWords.add(normalKeyWordArgs[i].trim());
					}
				}
			}
		}
		// 判断主关键字是否符合筛选条件
		String ruleKds = keyWord.replaceAll("，", ",");
		List<String> ruleKeyWords = new LinkedList<String>();
		for (String ruleKey : ruleKds.split(",")) {
			if (ruleKey != null && !ruleKey.trim().equals("")) {
				ruleKeyWords.add(ruleKey.trim());
			}
		}
		boolean result = contentKeyWords.containsAll(ruleKeyWords);
		if (result) {
			flag = true;
		}
		return flag;
	}
}
